package com.dd.census.service.impl;

import com.dd.census.entity.*;
import com.dd.census.mapper.*;
import com.dd.census.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务层实现类
 * MemberServiceImpl
 * @author whn
 * @date 2020/11/26
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
	MemberMapper mapper;

    @Override
    public int insert(Member member) {
        return mapper.insert(member);
    }

    @Override
    public int batchInsert(List<Member> list) {
    	return mapper.batchInsert(list);
    }

    @Override
    public int update(Member member) {
    	return mapper.update(member);
    }

    @Override
    public int delete(Member member) {
    	return mapper.delete(member);
    }

    @Override
    @Transactional
    public int batchDelete(List<String> list) {
        return mapper.batchDelete(list);
    }

	@Override
	public Member selectByKey(String id) {
		return mapper.selectByKey(id);
	}

	@Override
	public List<Member> selectList(Member member) {
		return mapper.selectList(member);
	}

	@Override
	public Map<String, Object> selectPage(Member member) {
		Map<String, Object> map = new HashMap<String, Object>();

		int total = this.total(member);
		
		map.put("pageCount", Math.ceil((double) total / member.getPageSize()));// 总页数
		map.put("total", total);// 总条数
		map.put("index", member.getPageIndex());// 当前页

		if (total > 0) {
			member.setPageIndex(member.getPageSize() * (member.getPageIndex() - 1));
			map.put("items", mapper.selectPage(member));
		}

		return map;
	}

	@Override
	public int total(Member member) {
		return mapper.total(member);
	}

	@Override
	@Transactional
	public int batchUpdate(Member member) {
		Member member2 = new Member();
		if(!StringUtils.isEmpty(member.getIds())) {
			for (Integer id : member.getIds()) {
				member2 = new Member();
				member2.setId(id);
				member2.setStatus(member.getStatus());
				mapper.update(member2);
			}
		}
		
		return 1;
	}
}